// pages/silver/index.js
const app = getApp()

import {
  IncomeCostModel
} from '../../models/incomeCost.js'
let incomeCostModel = new IncomeCostModel()

import {
  InviteUserModel
} from '../../models/inviteUser.js'
let inviteUserModel = new InviteUserModel()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    receiverId: '',
    senderId: 0,
    adminReceiverId: 0,
    receiveRecords: [],
    sendRecords: [],
    adminReceiveRecords: [],
    currentSearchPhone: '',
    count: 0,
    inviteeRecords: [],
    inviteeCount: 0,
    functionInfo: "查询用户银豆被赠送记录",
    currentSearchType : 1     // 1:银豆赠送记录 2： 邀请记录
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options)
    if(options.userId && options.userId != ''){
      this.setData({
        receiverId: options.userId
      })
    }
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // this.getSilverReceiveRecords()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 获取被赠送人id
  bindReceiverInput(e) {
    this.setData({
      receiverId: e.detail.value
    })
  },
  // 根据用户id 查询用户被赠送记录
  getSilverReceiveRecords() {
    // console.log(this.data.receiverId)
    const income = incomeCostModel.getSilverReceiveRecords(this.data.receiverId)
    income.then(
      res => {
        console.log(res.data)
        if (res.code == 1) {
          this.setData({
            currentSearchType: 1,
            functionInfo: "查询用户银豆被赠送记录",
            inviteeRecords: [],
            inviteeCount: 0,
            receiveRecords: res.data,
            count: res.data.length,
            receiverId: ''
          })
        }
        if(res.data.length >= 1){
          this.setData({
            currentSearchPhone: res.data[0].receiverPhone,
            count: res.data.length
          })
        }
        // console.log(res.data.length)
      }
    ).catch(e => {
      console.log(e)
    })
  },
  // 根据手机号查询用户被赠送记录
  getSilverReceiveRecordsByPhone(e){
    // console.log(e)
    // console.log(e.currentTarget.dataset.phone)
    if (e.currentTarget.dataset.phone =="13653180009"){
      return
    }
    const income = incomeCostModel.getSilverReceiveRecordsByPhone(e.currentTarget.dataset.phone)
    income.then(
      res => {
        // console.log(res.data)
        if (res.code == 1) {
          this.setData({
            currentSearchType: 1,
            functionInfo: "查询用户银豆被赠送记录",
            inviteeRecords: [],
            inviteeCount: 0,
            receiveRecords: res.data,
            count: res.data.length
          })

          if (res.data.length >= 1) {
            this.setData({
              currentSearchPhone: res.data[0].receiverPhone
          
            })
          }
        }
        
        // console.log(res.data.length)
        // console.log(this.data.receiveRecords)
      }
    ).catch(e => {
      console.log(e)
    })
  }

  ,
  // 根据邀请人id 查询用户邀请人列表
  getInviteeRecordsByInviterId(e){
    // console.log(e) 
    // console.log(e.currentTarget.dataset.senderid)
    const invite = inviteUserModel.getInviteeRecordsByInviterId(e.currentTarget.dataset.senderid)

    invite.then(
      res => {
        if(res.code == 1){
          // console.log(res.data)
          this.setData({
            currentSearchType: 2,
            functionInfo: "查询用户邀请记录",
            inviteeRecords: [],
            count: 0,
            currentSearchPhone: e.currentTarget.dataset.phone,
            inviteeRecords: res.data
          })

          if(res.data.length >= 1){
            this.setData({
              inviteeCount: res.data.length
            })
          }
        }
      }
    ).catch(e => {
      console.log(e)
    })

  }

})